package com.xiyu.service.repository.bed.building;

import com.xiyu.service.model.bed.building.BuildingInfo;
import com.xiyu.service.model.bed.building.BuildingInfoTable;
import com.xiyu.service.vo.bed.buildingInfo.BuildingInfoQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;

import java.util.Optional;

public interface BuildingInfoRepository extends JRepository<BuildingInfo, Long> {
    BuildingInfoTable buildingInfoTable = BuildingInfoTable.$;

    Optional<BuildingInfo> findFirstByBuildingName(String buildingName);

    Optional<BuildingInfo> findFirstByBuildingNameAndIdNot(String buildingName, Long id);

    default Page<BuildingInfo> pageQuery(BuildingInfoQueryInput inputVO){
        return sql().createQuery(buildingInfoTable)
                .whereIf(StringUtils.hasText(inputVO.getBuildingName()), () ->  buildingInfoTable.buildingName().like(inputVO.getBuildingName()))
                .orderBy(buildingInfoTable.id().desc())
                .select(buildingInfoTable)
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }


}